package com.ruifu.bom.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.bom.service.MaterielService;
import com.ruifu.common.eneity.BaseEntityPlus;
import com.ruifu.common.po.Materiel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 物料控制层
 *
 * @author nyr
 * @create 2019/12/23
 */
@RestController
@CrossOrigin
@RequestMapping("/materiel")
public class MaterielController {
    @Autowired
    private MaterielService materielService;

    /**
     * 校验物料编号是否可用
     *
     * @param mlCode
     * @return
     */
    @GetMapping("/checkoutByMlCode/{mlCode}")
    public R checkoutByMlCode(@PathVariable String mlCode) {
        try {
            //根据物料编号和账套id查询物料是否存在
            int count = materielService.count(Wrappers.<Materiel>lambdaQuery()
                    .eq(Materiel::getMlCode, mlCode)
                    .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()));
            if (count != 0) {
                return R.failed("物料编号已存在");
            }
            return R.ok("物料编号可用");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }
 }
